package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.config.dto.ConfigAddCmd;
import com.nidu.demo.config.dto.ConfigListQry;
import com.nidu.demo.config.dto.ConfigUpdateCmd;
import com.nidu.demo.config.api.SysConfigService;
import com.nidu.demo.config.dto.ConfigCO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 系统配置控制器
 *
 * @author auto
 */
@Tag(name = "管理后台 - 系统配置", description = "系统配置相关接口")
@RestController
@RequestMapping("/system/config")
@RequiredArgsConstructor
public class SysConfigController {

    private final SysConfigService service;

    @Operation(summary = "添加配置", description = "添加新的系统配置")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:config:create')")
    public SingleResponse<ConfigCO> addConfig(@RequestBody ConfigAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新配置", description = "更新系统配置信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:config:update')")
    public SingleResponse<ConfigCO> updateConfig(@PathVariable Long id, @RequestBody ConfigUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除配置", description = "删除指定的系统配置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:config:delete')")
    public Response deleteConfig(@Parameter(description = "配置ID") @PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除配置", description = "批量删除系统配置")
    @DeleteMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:config:delete')")
    public Response deleteConfigs(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));    }

    @Operation(summary = "获取配置详情", description = "根据ID获取配置详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:config:query')")
    public SingleResponse<ConfigCO> getConfig(@Parameter(description = "配置ID") @PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "根据键名获取配置", description = "根据配置键名获取配置信息")
    @GetMapping("/key/{key}")
    @PreAuthorize("@ss.hasPermission('system:config:query')")
    public SingleResponse<ConfigCO> getConfigByKey(@Parameter(description = "配置键名") @PathVariable String key) {
        return service.getByKey(key);
    }

    @Operation(summary = "查询配置列表", description = "分页查询系统配置列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:config:query')")
    public MultiResponse<ConfigCO> listConfigs(ConfigListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "更新配置状态", description = "更新配置的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:config:update')")
    public Response updateConfigStatus(@Parameter(description = "配置ID") @PathVariable Long id,
                                     @Parameter(description = "状态值(0:禁用,1:启用)") @RequestParam Integer status) {
        return service.updateStatus(id, status);
    }
} 